<template>
  <view class="flex-col">

    <view :class="boole?'bottom115':''">
      <apply-message v-model="ruleForm"/>
      <residual ref="refResidual" v-model="ruleForm" :boole="boole"/>
    </view>

    <view class="button-container" v-if="boole">
      <button class="btn-item" @click="submit">提 交</button>
    </view>

  </view>
</template>

<script>
import * as apis from '@/api/routineHandle'
import applyMessage from "./components/applyMessage.vue"
import residual from "./components/residual.vue"

export default {
  name: 'editSalvage',
  components: {applyMessage, residual},
  computed: {},
  watch: {},
  data() {
    return {
      ruleForm: {
        commitResidualValue: '',
        specification: '',
        attachmentOss: ''
      },
      boole: false
    };
  },
  onLoad(op) {
    if (op?.type === "edit") {
      this.boole = true;
    }
    if (op?.type === "view") {
      this.boole = false;
    }
    this.handleDetail(op?.id, op?.residualValueId)
  },
  methods: {
    async handleDetail(id, residualValueId) {
      this.loading = true
      try {
        if (id) {
          const res = await apis?.vehicleDisposalDetails(id)
          for (let key in res.data) {
            this.$set(this.ruleForm, key, res.data[key])
          }
        }
        if (residualValueId) {
          this.ruleForm = (await apis?.getResidualValueLog(residualValueId)).data
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    async submit() {

      try {
        await this.$refs.refResidual.handleEffect()

        uni.showLoading();

        let res = await apis?.residualValueLog(this.ruleForm);

        if (res.code === 200) {
          uni.$emit("refBackList", true);
          this.$modal.msgSuccess("提交成功");
          setTimeout(() => {
            this.$tab.navigateBack();
          }, 1000);
        }
      } catch (error) {
        if (typeof error === 'string') {
          this.$nextTick(() => {
            uni.pageScrollTo({
              selector: error,
            });
          });
        } else {
          console.error(error)
        }
      } finally {
        setTimeout(()=>{uni.hideLoading()},1000)
      }
    }
  },
};
</script>

<style scoped lang=""></style>


